<?php

/**
 * 物流管理
 * Enter description here ...
 * @author Admin
 *
 */
class ExpressAction extends CommonAction{

	
	/**
	 * 物流列表
	 * Enter description here ...
	 */
	public function index() {
		$data = Init_GP(array("start","end","name"));  //检索条件
		
		$express = D('Express');
		if (!empty($data)){
			if (!empty($data['start']) && !empty($data['end']))	{
				$start_tmp = strtotime($data['start']);
				$end_tmp = strtotime($data['end']);
				$map['addtime']  = array('between',"{$start_tmp},{$end_tmp}");
			}
			if (!empty($data['name']))$map['name']=array('eq',$data['name']);
		}
		
		import("@.ORG.Page"); //导入分页类
		$count = $express->where($map)->count();    //计算总数
		$p = new Page ($count,10);
		$expressdata=$express->where($map)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		//分页条件写入
		foreach($data as $key=>$val) {
			$p->parameter   .=   "$key=".$val."&";
		}
		
		
		$page = $p->show ();
		$this->assign("page",$page);
		$this->assign("expressdata",$expressdata);
		$this->assign('down',$p->parameter);
		
		$this->display();
	}
	
	
	
	/**
	 * 删除指定记录
	 * @see CommonAction::del()
	 */
	 public function del()   {
        $express = D('Express');  //订单物流
        $order = D('Order'); //订单
        $id       = GetNum($_GET['id']);  //主键
        
        $data = $express->find($id);
        if($express->delete($id)){
        	$map['id']=array('eq',$data['oid']);
        	$order->where($map)->setField('state',1);  //删除物流信息后同步订单物流状态
             $this->success("删除成功");
        }else {
             $this->error("删除失败");
        }
    }
    
    public function dels(){
    	$this->error("操作错误");
    }
	
	
	
    
    /**
     * 订单配送物流
     * @see CommonAction::add()
     */
	public function add(){
		$id = GetNum($_GET['id']);
		if (!$id)$this->error("操作错误");
		
		$order = D('Order');
		$data = $order->find($id);  //获取订单
		
		if ($data['state'] != 1)  //订单状态
			$this->error("此订单已发货");
			
		$this->assign('data',$data);
		$this->display();
	}
	
	
	
	/**
	 * 保存订单配送信息
	 * @see CommonAction::insert()
	 */
    public function insert($data){
		$express = D('Express');  //订单物流配送
		$order = D('Order');  //订单
		
		$data = Init_GP(array("oid","aid","name","goodsname","company","number","factoryid"));
		$data = $express->create($data,1);

		if(empty($data) || $data === false) {
			$this->error($express->getError());
		}
		
		//保存当前数据对象
		$id = $express->add($data);
		if($id) { //保存成功
			$map['id']=array('eq',$data['oid']);  
			$order->where($map)->setField('state',2);   //订单配送成功更改订单状态
			
			$this->assign('jumpUrl','__URL__/index');
			$this->success("添加成功");
		}else {
			//失败提示
			$this->error($model->getError());
		}
	}
    
    
    
	/**
	 * 修改物流配送信息
	 * @see CommonAction::update()
	 */
	public function update(){
		$data = Init_GP(array("id","name","goodsname","company","number","factoryid"));
		$map['id'] = array('eq',$data['id']);
		
		parent::update($data,$map);
	}
    
	
	
	/**
	 * 下载数据
	 * Enter description here ...
	 */
	public function down(){
		$data = Init_GP(array("start","end","name"));
		
		$express = D('Express');
		if (!empty($data)){
			if (!empty($data['start']) && !empty($data['end']))	{
				$start_tmp = strtotime($data['start']);
				$end_tmp = strtotime($data['end']);
				$map['addtime']  = array('between',"{$start_tmp},{$end_tmp}");
			}
			if (!empty($data['name']))$map['name']=array('eq',$data['name']);
			
		}
		$expressdata=$express->where($map)->order('id desc')->findAll();
		
		$keynames = array(
			'id'=>'ID',
			'name'=>'用户名',
			'goodsname'=>'商品名称',
			'company'=>'物流公司',
			'number'=>'物流单号',
			'addtime'=>'登陆时间'
		);
		
		down_xls($expressdata, $keynames,'物流信息_'.date("Y-m-d"));
	}
	

	
	
	
	
	
	/**
	 * 选择物流厂商
	 * Enter description here ...
	 */
	public function viewFactorys(){
		$expressFactory = D('Express_factory');
		$data = $expressFactory->order('id asc ') ->findAll();
		
		$this->assign("data",$data) ;
		
		$this->display();
	}
	
	
	
	
	
	
	
	
	
	
   
}
?>